package com.wcgksoft.controller.openAccount;

import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.system.Enterprise;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.EnterpriseProjectService;
import com.wcgksoft.service.openAccount.OpenAccountService;
import com.wcgksoft.service.system.EnterpriseService;
import com.wcgksoft.service.system.SysRoleService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * @ClassName OpenAccountController
 * @Description 开设管理账户
 * @Author user
 * @Date 2020/3/9 13:41
 * @Version V1.0
 **/
@RequestMapping("system/openAccount")
@Controller
public class OpenAccountController {

    @Autowired
    private OpenAccountService openAccountService;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private EnterpriseProjectService enterpriseProjectService;

    @Autowired
    private EnterpriseService enterpriseService;

    @ResponseBody
    @RequestMapping(value = "getDepart")
    public List<EnterpriseProject> getDepart() {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseProject.class);
        criteria.add(Restrictions.eq("parentId", -1));
        criteria.add(Restrictions.eq("enterprise",sysUser.getEnterprise()));
        List<EnterpriseProject> list = enterpriseProjectService.findByConditions(criteria);
        return list;
    }

    /**
     * 开设账号
     * */
    @ResponseBody
    @RequestMapping(value = "openAccount")
    public Layui openAccount(HttpServletRequest request,
                             @RequestParam(value = "data", required = false)String data)
    {
        JSONObject param = JSONObject.fromObject(data);
//        System.out.println(param);
        if (param == null)
        {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            SysUser sysUser = (SysUser) JSONObject.toBean(JSONObject.fromObject(data), SysUser.class);
            String aids= param.get("areaId").toString();
            String[] strArr = aids.split(",");
            HashSet<EnterpriseProject> set = new HashSet<>();
            for (int i = 0; i < strArr.length; i++)
            {
                int aid=Integer.parseInt(strArr[i]);
                EnterpriseProject byId = enterpriseProjectService.findById(aid);
                set.add(byId);
                sysUser.setEnterpriseProjects(set);
            }
            String rids=(String)param.get("enterprise");
            Integer rid=Integer.parseInt(rids);
            Enterprise byId = enterpriseService.findById(rid);
            String rolId =(String)param.get("role_name");
            Integer roleId=Integer.parseInt(rolId);
            sysUser.setStatus(1);
            sysUser.setSysRole(sysRoleService.findById(roleId));
            sysUser.setEnterprise(byId);
            sysUser.setCreateDate(new Date());
            openAccountService.save(sysUser);
            return Layui.data(0, "开设账号成功",1, Layui.addList(sysUser));
        }
    }

    @ResponseBody
    @RequestMapping(value = "upDateAccount")
    public Layui updateAccount(HttpServletRequest request,
                             @RequestParam(value = "data", required = false)String data)
    {
        JSONObject param = JSONObject.fromObject(data);
        if (param == null)
        {
            return Layui.data(1, "请求格式有误", 0, null);
        } else {
            SysUser sysUser = (SysUser) JSONObject.toBean(JSONObject.fromObject(data), SysUser.class);
            String aids= param.get("areaId").toString();
            String[] strArr = aids.split(",");
            HashSet<EnterpriseProject> set = new HashSet<>();
            for (int i = 0; i < strArr.length; i++)
            {
                int aid=Integer.parseInt(strArr[i]);
                EnterpriseProject byId = enterpriseProjectService.findById(aid);
                set.add(byId);
                sysUser.setEnterpriseProjects(set);
            }
            String companyName=(String)param.get("companyName");
            sysUser.setCompanyName(companyName);
            String rids=(String)param.get("enterprise");
            Integer rid=Integer.parseInt(rids);
            Enterprise byId = enterpriseService.findById(rid);
            String rolId =(String)param.get("roleId");
            Integer roleId=Integer.parseInt(rolId);
            sysUser.setStatus(1);
            sysUser.setSysRole(sysRoleService.findById(roleId));
            sysUser.setEnterprise(byId);
            sysUser.setCreateDate(new Date());
            openAccountService.saveMerge(sysUser);
            return Layui.data(0, "修改账号成功",1, Layui.addList(sysUser));

        }
    }
    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "page", required = false) Integer page,
                         @RequestParam(value = "limit", required = false) Integer limit)
    {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        System.out.println(sysUser.getEnterprise());
        DetachedCriteria criteria = DetachedCriteria.forClass(SysUser.class);
        criteria.add(Restrictions.eq("enterprise",sysUser.getEnterprise()));
        criteria.createAlias("sysRole","role");
        criteria.createAlias("role.sysRoleCode","rolecode");
        criteria.add(Restrictions.eq("rolecode.role_number","manage"));
        List<SysUser> list = openAccountService.findByConditions(criteria, page, limit);
        return Layui.data(0, "查询成功", openAccountService.getTotalCount(), list);
    }

    @ResponseBody
    @RequestMapping(value = "deleteById")
    @Transactional
    public Layui deleteById(@RequestParam(value = "pid",required = false)Integer id)
    {
        SysUser byId = openAccountService.findById(id);
        openAccountService.deleteCass(byId);
        return Layui.data(0, "删除成功", 1,null);
    }

    @ResponseBody
    @RequestMapping(value = "findProOne")
    public Layui findProOne(@RequestParam(value = "pid")Integer id)
    {
        EnterpriseProject byId = enterpriseProjectService.findById(id);
        return Layui.data(0, "查询项目成功", Layui.addList(byId));
    }

    /**
     * 根据名字查询用户信息
     */
    @ResponseBody
    @RequestMapping(value = "findByName")
    public Layui findByName(
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "keyWord",required = false)String name)
    {
        DetachedCriteria criteria = DetachedCriteria.forClass(SysUser.class);
        if(StringUtils.isNotEmpty(name)) {
            criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
        }
        List<SysUser> list = openAccountService.findByConditions(criteria,page,limit);
        return Layui.data(0, "查询成功", openAccountService.getTotalCount(),list);
    }




}
